// 导入外部模块
const { customRequire } = require('./utils.js');
const { Page } = customRequire('playwright');

class OrderListPage {
    /**
     * @param {Page} page - Playwright page object
     */
    constructor(page) {
        this.page = page;
        // 导入withRetry函数
        const { withRetry } = require('./playwrightUtils');
        this.withRetry = withRetry;
    }

    /**
     * Navigate to the order list page
     * @returns {Promise<void>}
     */
    async enter_order_list_page() {
        await this.page.getByRole('link', { name: 'Order' }).click();
        await this.page.getByRole('link', { name: 'Order List', exact: true }).click();
    }

    /**
     * Search for a specific order number
     * @param {string} orderNo - The order number to search for
     * @returns {Promise<void>}
     */
    async search_order_no(orderNo) {
        // UAT选择Order List Tab
        await this.page.getByRole('tab', { name: 'Order List', exact: true }).locator('span').click();

        const orderNoXpath = '//*[@id="order-list-tab"]/div[1]/form/div[1]/div[1]/div[1]/div/div/div/div/input';
        await this.page.locator(orderNoXpath).first().click();
        await this.page.locator(orderNoXpath).first().fill(orderNo);
        await this.page.locator('li').filter({ hasText: orderNo }).click();
        // await this.page.getByText(orderNo).click();
        await this.page.getByRole('button', { name: 'Search' }).click();
        await this.page.waitForTimeout(1000);
    }

    /**
     * Click on the first order with the specified order number
     * @param {string} orderNo - The order number to click on
     * @returns {Promise<void>}
     */
    async click_first_order_no(orderNo) {
        const firstRowOrderNoXpath = '//*[@id="pane-orderListTab"]/div/div/div[1]/div/div[2]/div[3]/table/tbody/tr/td[2]/div/a';
        const text = await this.page.locator(firstRowOrderNoXpath).textContent();
        if(text == orderNo){
            await this.page.getByRole('cell', { name: `${orderNo}` }).locator('a').first().click();
        }else{
            // await this.page.locator('button[name="Search"]').click();
            await this.page.getByRole('button', { 
                name: 'Search',
                includeHidden: false // 默认排除不可见元素
            }).first().click();
        }
    }

    /**
     * Search for multiple order numbers in batch
     * @param {string[]} orderNo - Array of order numbers to search for
     * @returns {Promise<void>}
     */
    async search_order_no_batch(orderNo) {
        if (!orderNo || !Array.isArray(orderNo)) {
            throw new Error('Invalid order numbers format');
        }

        // UAT选择Order List Tab
        await this.withRetry(this.page, null, async () => {
            await this.page.getByRole('tab', { name: 'Order List', exact: true }).locator('span').click();
        }, '点击Order List Tab');
        
        const orderNoString = orderNo.join('\n');
        
        // 使用withRetry机制处理批量订单号输入
        await this.withRetry(this.page, null, async () => {
            // 先尝试滚动到页面顶部，避免导航栏遮挡
            await this.page.evaluate(() => {
                window.scrollTo(0, 0);
                // 隐藏可能遮挡的导航栏元素
                const navbar = document.querySelector('.collapse.navbar-collapse');
                if (navbar) {
                    navbar.style.display = 'none';
                }
            });
            
            await this.page.waitForTimeout(500);
            
            // 尝试多种方式定位和点击批量输入框
            const batchInput = this.page.getByPlaceholder('Please input Order No.(Batch)', { exact: true }).first();
            
            // 确保元素可见并可交互
            await batchInput.waitFor({ state: 'visible' });
            await batchInput.scrollIntoViewIfNeeded();
            
            // 使用force点击避免被遮挡
            await batchInput.click({ force: true });
            await this.page.waitForTimeout(300);
            
            // 清空并填入订单号
            await batchInput.fill('');
            await batchInput.fill(orderNoString);
        }, '填写批量订单号');

        // 点击搜索按钮
        await this.withRetry(this.page, null, async () => {
            await this.page.getByRole('button', { name: 'Search' }).click();
        }, '点击搜索按钮');

        // 修改显示条目100/page
        await this.withRetry(this.page, null, async () => {
            await this.page.getByRole('textbox', { name: 'Select', exact: true }).click();
            await this.page.getByRole('listitem').filter({ hasText: '100/page' }).locator('span').click();
        }, '设置显示条目为100/page');
    }

    /**
     * Search for a specific TRF number
     * @param {string} trfNo - The TRF number to search for
     * @returns {Promise<void>}
     */
    async search_TRF_no(trfNo) {
        await this.page.getByPlaceholder('Please input TRF No', { exact: true }).first().click();
        await this.page.getByPlaceholder('Please input TRF No', { exact: true }).first().fill(trfNo);
        await this.page.getByText(trfNo).click();
        await this.page.getByRole('button', { name: 'Search' }).click();
    }

    /**
     * Enter quotation confirm date
     * @returns {Promise<void>}
     */
    async enter_quotation_confirm_date() {
        // Implement the method as needed
    }

    /**
     * Choose applicant
     * @returns {Promise<void>}
     */
    async choose_applicant() {
        // Implement the method as needed
    }
}

module.exports = OrderListPage;